package com.qmx.qmxapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.qmx.qmxapp.R;
import com.qmx.qmxapp.base.BaseActivity;
import com.qmx.qmxapp.callback.DialogCallback;
import com.qmx.qmxapp.constant.AppConstant;
import com.qmx.qmxapp.constant.ComParamContact;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CodeActivity extends BaseActivity {

    @BindView(R.id.next)
    TextView next;
    @BindView(R.id.send_code)
    TextView send_code;
    @BindView(R.id.edit_phone)
    EditText edit_phone;
    private TextView tip_content;

    private String mobile = "";
    private String captcha = "";

    /**
     * 加验证码计时器
     *
     * @param savedInstanceState
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_code);
        ButterKnife.bind(this);
        tip_content = findViewById(R.id.tip_content);
        tip_content.setText("填写验证码");
        next = findViewById(R.id.next);
        mobile = this.getIntent().getStringExtra("mobile");
    }


    public String getCaptcha() {
        String captcha = edit_phone.getText().toString();
        if (TextUtils.isEmpty(captcha)) {
            showToast("请输入验证码");
            return "";
        }
        return captcha;
    }

    @OnClick({R.id.next, R.id.send_code})
    public void onclick(View view) {
        Intent i = new Intent();
        switch (view.getId()) {
            case R.id.send_code:
                sendCode();
                break;
            case R.id.next:
                smsCheck();
                i.putExtra("mobile", mobile);
                i.setClass(this, UserLoginSetActivity.class);
                startActivity(i);
                break;
        }

    }

    public void sendCode() {
        OkGo.<String>post(AppConstant.Url + ComParamContact.sendValidCode.PATH)
                .params(ComParamContact.sendValidCode.AREA_CODE, "+86")
                .params(ComParamContact.sendValidCode.MOBILE, mobile)
                .params(ComParamContact.sendValidCode.EVENT, "register")
                .execute(new DialogCallback<String>(this) {

                    @Override
                    public void onSuccess(Response<String> response) {
                        showToast(response.body());
//                            handleResponse(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        showToast("接口有问题");
//                            handleError(response);
                    }
                });
    }

    public void smsCheck() {
        getCaptcha();
        OkGo.<String>post(AppConstant.Url + ComParamContact.smsCheck.PATH)
                .params(ComParamContact.smsCheck.AREA_CODE, "+86")
                .params(ComParamContact.smsCheck.MOBILE, mobile)
                .params(ComParamContact.smsCheck.EVENT, "register")
                .params(ComParamContact.smsCheck.CAPTCHA, getCaptcha())
                .execute(new DialogCallback<String>(this) {

                    @Override
                    public void onSuccess(Response<String> response) {
                        showToast(response.body());
//                            handleResponse(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        showToast("接口有问题");
//                            handleError(response);
                    }
                });
    }
}
